<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <!-- 搜索 -->
      <div class="filter-item">商品售罄率統計時間:</div>
      <date-range-picker
        v-model="modelEndDate"
        clearable
        placeholder="时间"
        class="filter-item"
        :fix-date.sync="timeSlot"
        @keyup.enter.native="crud.toQuery"
        @change="getTimeSlot"
      />
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation>
        <template #right>
          <el-button
            class="filter-item"
            size="mini"
            type="warning"
            :disabled="modelEndDate.length < 1"
            @click="exportSoldOutEvaluation(crud.selections)"
          >導出
          </el-button>
          <el-button
            class="filter-item"
            size="mini"
            type="primary"
            @click="openProduct"
          >選擇商品
          </el-button>
          <choose-product ref="chooseProduct" @childNotify="childNotify" />
          <rrOperation :crud="crud" />
        </template>
      </crudOperation>
      <!--表格渲染-->
      <el-table
        ref="table"
        v-loading="crud.loading"
        row-key="id"
        :data="crud.data"
        stripe
        style="width: 100%;"
        @selection-change="crud.selectionChangeHandler"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column prop="sku" label="产品编号" width="100"/>
        <el-table-column prop="upc" label="upc" />
        <el-table-column prop="productName" label="产品名称" width="200" show-tooltip-when-overflow />
        <el-table-column prop="stock" label="	库存" align="right" />
        <el-table-column prop="thirtyDaySale" label="30均销量" align="right" />
        <el-table-column prop="fifteenDaySale" label="15均销量" align="right" />
        <el-table-column prop="productCategory" label="产品种类" />
        <el-table-column prop="price" label="售价" align="right" />
        <el-table-column prop="cost" label="成本价" align="right" />
        <el-table-column prop="grossPr" label="毛利率" align="right" />
        <el-table-column prop="sellThroughRate" label="月售罄率" align="right" />
        <el-table-column prop="availableMonths" label="可售月數" align="right" />
        <el-table-column prop="vendor" label="供应商" width="150" show-tooltip-when-overflow/>
        <el-table-column prop="consignment" label="是否寄卖" />
        <el-table-column prop="str" label="STR_%" />
        <el-table-column prop="totalValue" label="總貨值" align="right" />
        <el-table-column prop="weight" label="Weight" align="right" />
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudReport from '@/api/fms/report'
import CRUD, { presenter, header, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import pagination from '@crud/Pagination'
import { mapGetters } from 'vuex'
import { downloadFile } from '@/utils'
import dateRangePicker from '@/components/DateRangePicker'
import chooseProduct from '@/components/product/index'

export default {
  name: 'SoldOutEvaluation',
  components: { pagination, crudOperation, rrOperation, dateRangePicker, chooseProduct },
  mixins: [presenter(), header(), crud()],
  dicts: ['true_false'],
  cruds() {
    return CRUD({
      title: '訂單銷售統計表',
      url: 'report/soldOutEvaluation',
      sort: [],
      crudMethod: { ...crudReport },
      optShow: {},
      query: { startTime: '' },
      queryOnPresenterCreated: false
    })
  },
  data() {
    return {
      modelEndDate: [],
      timeSlot: ''
    }
  },
  computed: {
    ...mapGetters(['imagesUploadApi', 'baseApi'])
  },
  created() {
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    exportSoldOutEvaluation() {
      crudReport.exportSoldOutEvaluation().then(res => {
        if (res.code !== undefined && res.code !== 0) {
          this.$notify({
            title: '失敗',
            message: res.message,
            type: 'error'
          })
          return
        }
        downloadFile(res, this.crud.title + '数据', 'xlsx')
      })
    },
    getTimeSlot(sel) {
      this.query.startTime = sel[0]
      this.query.endTime = sel[1]
      if (this.query.productIds.length < 1) {
        this.$notify({
          title: '失敗',
          message: '請選擇產品列表',
          type: 'warning'
        })
        return
      }
      this.crud.toQuery()
    },
    openProduct() {
      this.$refs.chooseProduct.open()
    },
    childNotify(params) {
      if (params.length === 0) {
        this.$message.error('請選擇商品')
      } else {
        this.query.productIds = []
        for (var item of params) {
          this.query.productIds.push(item.product.id)
        }
        this.$refs.chooseProduct.close()
        this.$refs.chooseProduct.refresh()
      }
    }
  }
}
</script>

<style scoped>

</style>
